/**
 * Class to help creating and animating texts.
 */
package org.openmobo.playn.poc1.core;

import static playn.core.PlayN.graphics;

import java.util.logging.Logger;

import org.jbox2d.collision.shapes.PolygonShape;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.FixtureDef;
import org.jbox2d.dynamics.World;

import playn.core.CanvasLayer;
import playn.core.Font;
import playn.core.Layer;
import playn.core.TextFormat;
import playn.core.TextLayout;

/**
 * @author leonardodinnouti
 *
 */
class Text {

	Layer layer;
	Body body;
	boolean animated = false;
	float physUnitPerScreenUnit = 1 / 26.666667f;
	TextLayout layout;
	Logger log = Logger.getLogger("Poc1");

	boolean isClicked(float x, float y) {
		log.info("Testing x=" + x + " y=" + y + 
				" against (" + layer.transform().translation().x() + "," + 
				layer.transform().translation().y() + ")");
		return (x > layer.transform().translation().x() - layout.width() / 2 && 
				x < layer.transform().translation().x() + layout.width() / 2 &&
				y > layer.transform().translation().y() - layout.height() / 2 &&
				y < layer.transform().translation().y() + layout.height() / 2);
	}
	
	public Text(World world, String text, float size, boolean animated, float x, float y) {
		// add some text to said soothing background
		Font font = graphics().createFont("Helvetica", Font.Style.BOLD, size);
		TextFormat format = new TextFormat().withFont(font);
		layout = graphics().layoutText(text,
				format);
		layer = createTextLayer(layout);

		// centering text
		layer.setOrigin(layout.width() * 0.5f, layout.height() * 0.5f);
		layer.setTranslation(x,y);
		float px = x * physUnitPerScreenUnit;
		float py = y * physUnitPerScreenUnit;
		this.animated = animated;
		if (animated) {
			BodyDef bodyDef = new BodyDef();
			bodyDef.type = BodyType.DYNAMIC;
			bodyDef.position = new Vec2(px, py);
			body = world.createBody(bodyDef);
			FixtureDef fixtureDef = new FixtureDef();
			PolygonShape shape = new PolygonShape();
			fixtureDef.shape = new PolygonShape();
			shape.setAsBox(layout.width() * physUnitPerScreenUnit * 0.5f, 
					layout.height() * physUnitPerScreenUnit * 0.5f);
			fixtureDef.shape = shape;
			fixtureDef.density = 1.0f;
			fixtureDef.friction = 0.03f;
			body.createFixture(fixtureDef);
			body.setTransform(new Vec2(px, py), 0f);
		}
	}

	protected Layer createTextLayer(TextLayout layout) {
		CanvasLayer layer = graphics().createCanvasLayer(
				(int) Math.ceil(layout.width()),
				(int) Math.ceil(layout.height()));
		layer.canvas().drawText(layout, 0, 0);
		return layer;
	}

	public void paint(float delta) {
		if (animated) {
			float px = body.getPosition().x / physUnitPerScreenUnit;
			float py = body.getPosition().y / physUnitPerScreenUnit;
			layer.setTranslation(px, py);
		}
	}
	
	public float height() {
		return layout.height();
	}

	public float width() {
		return layout.width();
	}

}

